package com.linxz.utils;

import android.annotation.SuppressLint;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.List;

public class FragmentHandler {
    final String TAG = FragmentHandler.class.getSimpleName();
    private FragmentManager manager;
    private int fragContainerId;
    private List<Fragment> fragments;

    public static Builder builder(){
        return new Builder();
    }

    @SuppressLint("CommitTransaction")
    private FragmentHandler(AppCompatActivity activity, int fragContainerId, List<Fragment> fragments, int defFrag){
        if (activity==null){
            throw new IllegalArgumentException(TAG +":activity can not be null!");
        }
        if (fragContainerId==0){
            throw new IllegalArgumentException(TAG +":frameLayout id is error!");
        }
        if (fragments==null || fragments.size()==0){
            throw new IllegalArgumentException(TAG +":fragments can not be empty");
        }
        this.fragContainerId=fragContainerId;
        this.fragments=fragments;
        manager= activity.getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.add(fragContainerId,fragments.get(defFrag)).commit();
    }


    @SuppressLint("CommitTransaction")
    public void showFrag(Fragment fragment){
        hideFrag();
        manager.beginTransaction().add(fragContainerId,fragment).commit();
    }

    @SuppressLint("CommitTransaction")
    public void showFrag(int index){
        hideFrag();
        if (index>=fragments.size()){
            throw new IllegalArgumentException(TAG+":该index超出fragment范围");
        }
        if (fragments.get(index).isAdded()){
            manager.beginTransaction().show(fragments.get(index)).commit();
        }else{
            manager.beginTransaction().add(fragContainerId,fragments.get(index)).show(fragments.get(index)).commit();
        }
    }

    private void hideFrag(){
        FragmentTransaction transaction=manager.beginTransaction();
        for (Fragment fragment:fragments){
            transaction.hide(fragment);
        }
        transaction.commit();
    }



    public static class Builder{
        private AppCompatActivity activity;
        private int fragContainerId;
        private List<Fragment> fragments;
        private int defFrag;
        public Builder withActivity(AppCompatActivity activity){
            this.activity=activity;
            return this;
        }
        public Builder withFrameLayoutId(int fragContainerId){
            this.fragContainerId=fragContainerId;
            return this;
        }
        public Builder withFragments(List<Fragment> fragments){
            this.fragments=fragments;
            return this;
        }
        public Builder withDefFrag(int defFrag){
            this.defFrag=defFrag;
            return this;
        }
        public FragmentHandler build(){
            return new FragmentHandler(activity,fragContainerId,fragments,defFrag);
        }
    }


}
